# -*- coding: UTF-8 -*-
"""
@Project ：swift-forge-engine 
@File    ：sys_menu.py
@IDE     ：PyCharm 
@Author  ：Tony.Gan
@Date    ：2025/2/24 1:24 
@Des     ：
"""
from typing import Annotated

from fastapi import APIRouter, Path, Query, Request, Depends
from fastapi_limiter.depends import RateLimiter

from App.app.admin.schemas.menu_schema import UpdateMenuParam, CreateMenuParam, GetMenuListDetails
from App.app.admin.services.menu_service import MenuService
from App.common.respones.response_schema import response_base, ResponseModel
from App.common.security.jwt import DependsJwtAuth
from App.utils.serializers import select_as_dict

router = APIRouter(prefix="/menu")


@router.get('/sidebar', summary='获取用户菜单展示树',
            dependencies=[Depends(RateLimiter(times=20, minutes=1)), DependsJwtAuth])
async def get_user_sidebar_tree(request: Request) -> ResponseModel:
    menu = await MenuService.get_user_menu_tree(request=request)
    return response_base.success(data=menu)


@router.get('/{pk}', summary='获取菜单详情', dependencies=[Depends(RateLimiter(times=20, minutes=1)), DependsJwtAuth])
async def get_menu(pk: Annotated[int, Path(...)]) -> ResponseModel:
    menu = await MenuService.get(pk=pk)
    data = GetMenuListDetails(**select_as_dict(menu))
    return response_base.success(data=data)


@router.get('', summary='获取所有菜单展示树', dependencies=[Depends(RateLimiter(times=20, minutes=1)), DependsJwtAuth])
async def get_all_menus() -> ResponseModel:
    menu = await MenuService.get_menu_tree()
    return response_base.success(data=menu)


@router.post(
    '',
    summary='创建菜单',
    dependencies=[Depends(RateLimiter(times=20, minutes=1)), DependsJwtAuth]
)
async def create_menu(obj_in: CreateMenuParam) -> ResponseModel:
    await MenuService.create(obj_in=obj_in)
    return response_base.success()


@router.put(
    '',
    summary='更新菜单',
    dependencies=[Depends(RateLimiter(times=20, minutes=1)), DependsJwtAuth]
)
async def update_menu(obj_in: UpdateMenuParam) -> ResponseModel:
    count = await MenuService.update(obj_in=obj_in)
    # print(obj_in)
    # count = 1
    if count > 0:
        return response_base.success()
    return response_base.fail()


@router.delete(
    '/{pk}',
    summary='删除菜单',
    dependencies=[Depends(RateLimiter(times=20, minutes=1)), DependsJwtAuth]
)
async def delete_menu(request: Request, pk: Annotated[int, Path(...)]) -> ResponseModel:
    count = await MenuService.delete(request=request, pk=pk)
    if count > 0:
        return response_base.success()
    return response_base.fail()
